package com.atguigu.day20.homework.h2;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/25 19:32
 * @注释 第一个子线程 打印奇数
 */
public class Number01 {
    private int num = 1;
    private boolean odd = true;

    public synchronized void printOddNum() {
        if (odd) {
            System.out.println(Thread.currentThread().getName() + "->" + num);
            num++;
        }
        odd = false;
        this.notify();
        try {
            this.wait();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

    public synchronized void printEvenNum() {
        if (!odd) {
            System.out.println(Thread.currentThread().getName() + "->" + num);
            num++;
        }
        odd = true;
        this.notify();

        try {
            this.wait();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public int getNum() {
        return num;
    }
}
